package physics;

/**
 *
 * @author peter
 */
public class PVector {
	private double x;
	private double y;


	// GETTERS & SETTERS
	public double getX()
	{
		return x;
	}

	public double getY()
	{
		return y;
	}

	public void setX(double x)
	{
		this.x = x;
	}

	public void setY(double y)
	{
		this.y = y;
	}

	public void set(PVector other)
	{
		x = other.x;
		y = other.y;
	}


	// CONSTRUCTORS
	public PVector()
	{
	}

	public PVector(double x, double y)
	{
		this.x = x;
		this.y = y;
	}

	public PVector(PVector other)
	{
		this(other.x, other.y);
	}

	// METHODS
	public double dot(PVector other)
	{
		return x * other.getX() + y * other.getY();
	}

	public PVector getNegative()
	{
		return new PVector(x * -1.0, y * -1.0);
	}

	public void add(PVector vec)
	{
		x += vec.getX();
		y += vec.getY();
	}

	public void sub(PVector vec)
	{
		x -= vec.getX();
		y -= vec.getY();
	}

	public PVector getSub(PVector vec)
	{
		return new PVector(x - vec.getX(), y - vec.getY());
	}

	public PVector getOpPerp()
	{
		return new PVector(y, -x);
	}

	public PVector getPerp()
	{
		return new PVector(-y, x);
	}

	public double getLength()
	{
		return (double) Math.sqrt(x * x + y * y);
	}

	public double getDistance(PVector vec)
	{
		double dx = vec.getX() - x;
		double dy = vec.getY() - y;
		return (double) Math.sqrt(dx * dx + dy * dy);
	}

	public void normalize()
	{
		double l = getLength();
		if (l == 0)
			return;
		x /= l;
		y /= l;
	}


	public PVector getNormal()
	{
		PVector retval = new PVector(this);
		double l = getLength();
		if (l == 0)
			return retval;
		retval.x /= l;
		retval.y /= l;
		return retval;
	}



	public String toString()
	{
		return "[ " + x + ", " + y + " ]" + "(" + getLength() + ")";
	}

	void scale(double factor)
	{
		x *= factor;
		y *= factor;
	}

	public PVector getScale(double factor)
	{
		return new PVector(x * factor, y * factor);
	}

	public PVector getSum(PVector other)
	{
		return new PVector(x + other.getX(), y + other.getY());
	}

	public PVector getCross(PVector other)
	{
		return new PVector(y * other.getX(), x * other.getY());

	}

	public static PVector crossProduct(PVector a, PVector b)
	{
		return new PVector(a.getX() * b.getY(), a.getY() * b.getX());
	}

	public static double dotProduct(PVector a, PVector b)
	{
		return a.getX() * b.getX() + a.getY() * b.getY();
	}
}
